﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.Storagelocation;
using Warehouse.Domain.Entities;
using Warehouse.Result;

namespace Warehouse.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StoragelocationController : ControllerBase
    {
        private readonly IStoragelocationAppService storagelocationService;

        public StoragelocationController(IStoragelocationAppService storagelocationService)
        {
            this.storagelocationService = storagelocationService;
        }

        /// <summary>
        /// 添加库位设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateStoragelocation(CreateStoragelocationInput input)
        {
            return storagelocationService.CreateStoragelocation(input);
        }


        /// <summary>
        /// 获取库位设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<GetStoragelocationOutput> GetStoragelocation([FromQuery]PageInput input)
        {
            return storagelocationService.GetStoragelocation(input);
        }

        /// <summary>
        /// 删除库位设置
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> DeleteStoragelocation(long Id)
        {
            return storagelocationService.DeleteStoragelocation(Id);
        }

        /// <summary>
        /// 修改库区    
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateStoragelocation(UpdateStoragelocationInput input)
        {
            return storagelocationService.UpdateStoragelocation(input);
        }

        /// <summary>
        /// 获取库区
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<StorageLocationModel> GetModelWareHouse(long Id)
        {
            return storagelocationService.GetModelWareHouse(Id);
        }

        /// <summary>
        /// 获取所有货架
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<StorageModel> ListStorage()
        {
            return storagelocationService.ListStorage();
        }

        /// <summary>
        /// 库位设置导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Derive()
        {
            var book = new XSSFWorkbook();
            var a = book.CreateSheet("标题");
            var row = a.CreateRow(0);
            row.CreateCell(0).SetCellValue("库区设置编号");
            row.CreateCell(1).SetCellValue("库区设置编码");
            row.CreateCell(2).SetCellValue("库区设置名称");
            row.CreateCell(3).SetCellValue("货架/库区编码");
            row.CreateCell(4).SetCellValue("描述");

            var index = 1;
            var bs = storagelocationService.GetStoragelocation(new PageInput { PageIndex = 1, PageSize = 1000 });
            foreach (var s in bs.PageDatas)
            {
                var rows = a.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(s.Id);
                rows.CreateCell(1).SetCellValue(s.StorageLocationCoding);
                rows.CreateCell(2).SetCellValue(s.StorageLocationName);
                rows.CreateCell(3).SetCellValue(s.StorageName);
                rows.CreateCell(4).SetCellValue(s.StorageLocationDescribe);
            }

            byte[] sb = null;
            using (var ms = new MemoryStream())
            {
                book.Write(ms);
                sb = ms.ToArray();
            }
            return File(sb, System.Net.Mime.MediaTypeNames.Application.Octet, "库位设置打印.xlsx");

        }




    }
}
